package com.guigu.qiniuyun.controller;

import com.guigu.qiniuyun.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
@Api(value="七牛云文件上传服务",tags = "七牛云文件上传服务",description = "七牛云文件上传服务")
public class QiniuyunController {
    @Autowired
    FileService fileService;

    //上传证件照
    @PostMapping("/upload")
    @ApiOperation("上传头像")
    public String upload(@ApiParam(value = "头像",required = true) @RequestParam("file") MultipartFile multipartFile) throws IOException {

        //调用fileService上传文件
        //生成的文件名称fileName，要保证它的唯一
        //文件原始名称
        String originalFilename = multipartFile.getOriginalFilename();
        //扩展名
        String suffix = originalFilename.substring(originalFilename.lastIndexOf(".")-1);
        //文件名称
        String fileName = UUID.randomUUID()+suffix;
        //byte[] bytes,String fileName
        return fileService.upload(multipartFile.getBytes(),fileName);
    }


}
